#include <bits/stdc++.h>

using namespace std;

//#define int long long
using ll = long long;
const int MAXN = 550;
int sum[MAXN + 1][MAXN + 1];
int n , m , k;
int check(int i, int mx)
{
    return min(max(i , 0), mx);
}
int pref(int i, int j)
{
    return sum[check(i,n)][check(j,m)];
}
void solve()
{
    cin >> n >> m >> k; k--;
    vector<string>mine(n);
    int all_gold = 0;
    for(int i = 0;i < n;i ++)
    {
        cin >> mine[i];
        for(int j = 0;j < m;j ++)
        {
            all_gold += (mine[i][j] == 'g');
        }
    }
    for(int i = 0;i < n;i ++)
    {
        for(int j = 0;j < m;j ++)
        {
            sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + (mine[i][j] == 'g');
        }
    }
    int ans = all_gold;
    for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (mine[i][j] == '.') {
				int a = i - k, b = i + k + 1, c = j - k, d = j + k + 1;
				ans = min(ans, pref(b, d) - pref(a, d) - pref(b, c) + pref(a, c));
			}
		}
	}
	cout << all_gold - ans << "\n";
}
signed main()
{
    int t; cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}